package com.escool.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.escool.model.TbGrado;
import com.escool.model.TbNivel;
import com.escool.model.TbColegio;
import com.escool.model.TbSede;
import com.escool.model.TbSedeHasSeccion;
import com.escool.service.ColegioService;
import com.escool.service.GradosService;
import com.escool.service.NivelesService;
import com.escool.service.RolService;
import com.escool.service.TurnoService;
import com.escool.service.VinSedeSeccionService;

@Controller
public class ColegioController {
	
	@Autowired
	ColegioService colegioService;

	@Autowired
	TurnoService turnoService;
	
	
	@Autowired
	VinSedeSeccionService VinSedeSeccionService;
	
	@RequestMapping("showColegio")
	public String showColegio(Map<String, Object> map){
		map.put("colegio", new TbColegio());
		map.put("colegioList", colegioService.listColegio());
		return "/colegio";
	}
	
	@RequestMapping(value = "addColegio", method = RequestMethod.POST)
	public String addRoles(@ModelAttribute("colegio") @Valid TbColegio tbColegio, BindingResult result){
		if (!result.hasErrors()) {
			try{
				colegioService.addColegio(tbColegio);
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			
			return "redirect:/showColegio";
		}
		return "";
	}
	
	@RequestMapping(value = "editColegio", method = RequestMethod.POST)
	public String editRoles(@ModelAttribute("colegio") @Valid TbColegio tbColegio, BindingResult result){
		if (!result.hasErrors()) {
			try{
				colegioService.updateColegio(tbColegio);	
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			
			return "redirect:/showColegio";
		}
		return "";
	}
	
	@RequestMapping(value = "deleteColegio", method = RequestMethod.POST)
	public String delRoles(@ModelAttribute("colegio") @Valid TbColegio tbColegio, BindingResult result){
		if (!result.hasErrors()) {
			try{
				colegioService.delColegio(tbColegio.getIdColegio());
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			
			return "redirect:/showColegio";
		}
		return "";
	}
	
	@RequestMapping(value = "mostrarSede", method = RequestMethod.POST)
	public String mostrarSede(Map<String, Object> map,@ModelAttribute("colegio") @Valid TbColegio tbColegio, BindingResult result){
		TbSede sede = new TbSede();
		sede.setTbColegio(tbColegio);
		map.put("sede", sede);
		map.put("sedeList", colegioService.listSede(tbColegio.getIdColegio()));
		return "/showSede";

	}

	
	@RequestMapping("showSede")
	public String showSede(Map<String, Object> map,HttpServletRequest request){
		String id = request.getParameter("idColegio");
		
		TbSede sede = new TbSede();
		sede.setTbColegio(colegioService.findColegio(Integer.parseInt(id)));
		map.put("sede", sede);
		map.put("sedeList", colegioService.listSede(Integer.parseInt(id)));
		return "/showSede";
	}
	

	@RequestMapping(value = "addSede", method = RequestMethod.POST)
	public String addSede(@ModelAttribute("sede") @Valid TbSede tbSede, BindingResult result){
		if (!result.hasErrors()) {
			try{
				colegioService.addSede(tbSede);
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			
			return "redirect:/showSede?idColegio="+tbSede.getTbColegio().getIdColegio();
		}
		return "";
	}
	
	@RequestMapping(value = "editSede", method = RequestMethod.POST)
	public String editSede(@ModelAttribute("sede") @Valid TbSede tbSede, BindingResult result){
		if (!result.hasErrors()) {
			System.out.println("datos " + tbSede.getIdSede());
			try{
				colegioService.updateSede(tbSede);
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			
			return "redirect:/showSede?idColegio="+tbSede.getTbColegio().getIdColegio();
		}
		return "";
	}
	
	@RequestMapping(value = "deleteSede", method = RequestMethod.POST)
	public String delSede(@ModelAttribute("sede") @Valid TbSede tbSede, BindingResult result){
		if (!result.hasErrors()) {
			try{
				colegioService.delSede(tbSede.getIdSede());
			}
			catch(DataAccessException ex){
				ex.printStackTrace();
			}
			
			return "redirect:/showSede?idColegio="+tbSede.getTbColegio().getIdColegio();
		}
		return "";
	}
	
	@RequestMapping("findProvincia")
	public String findProvincia(Map<String, Object> map,HttpServletRequest request){		
		map.put("listProvincia", colegioService.listProvincias());
		return "/findProvincia";
	}
	
	
	
	@RequestMapping(value = "showSedeSeccion_1", method = RequestMethod.POST)
	public String showSedeSeccion_1(Map<String, Object> map,@ModelAttribute("sede") @Valid TbSede tbSede, BindingResult result){
		System.out.println("entra");
		TbSedeHasSeccion tbsec = new TbSedeHasSeccion();
		tbsec.setTbSede(colegioService.findSede(tbSede.getIdSede()));
		map.put("VinSedeSeccion", tbsec);
		map.put("TbSedeHasSeccionList", VinSedeSeccionService.listTbSedeHasSeccion(tbSede.getIdSede()));
		map.put("turnoList", turnoService.listTurno());
		return "/vinSedeSeccion";
			
		
		
	}
	
}
